package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.MallCategory;
import com.cskaoyan.bean.pojo.MallCategoryExample;
import com.cskaoyan.bean.vo.CategoryCreateVo;
import com.cskaoyan.bean.vo.CategoryL1Vo;
import com.cskaoyan.bean.vo.CategoryListVo;
import com.cskaoyan.bean.vo.CategoryListVo1;
import com.cskaoyan.mapper.MallCategoryMapper;
import com.cskaoyan.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:商品类目
 * @Author: chaohe@com.cskaoyan
 * @Date: 2021/7/9
 * @Time: 16:30
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    MallCategoryMapper mallCategoryMapper;

    /**
     * @Author chaohe
     * @description: 商品类目的回显
     * @Date 14:59 2021/7/10
     * url: http://localhost:8083/admin/category/list
     */
    @Override
    public List<CategoryListVo> list() {
        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();

        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(0);

        //查询pid何delete都为0的数据
        List<MallCategory> mallCategoryList = mallCategoryMapper.selectByExample(mallCategoryExample);
        //用categoryListVoList回收数据
        List<CategoryListVo> categoryListVoList = new ArrayList<>();
        for (MallCategory mallCategory : mallCategoryList) {

            MallCategoryExample mallCategoryExample1 = new MallCategoryExample();
            MallCategoryExample.Criteria criteria1 = mallCategoryExample1.createCriteria();

            criteria1.andLevelEqualTo("L2");
            criteria1.andDeletedEqualTo(0);
            criteria1.andPidEqualTo(mallCategory.getId());

            List<MallCategory> mallCategoryList1 = mallCategoryMapper.selectByExample(mallCategoryExample1);
            List<CategoryListVo1> categoryListVo1List = new ArrayList<>();

            for (MallCategory category : mallCategoryList1) {
                CategoryListVo1 categoryListVo1 = new CategoryListVo1(category.getId(), category.getName(), category.getKeywords(), category.getDesc(), category.getIconUrl(), category.getPicUrl(), category.getLevel());
                categoryListVo1List.add(categoryListVo1);
            }
            CategoryListVo categoryListVo = new CategoryListVo(mallCategory.getId(), mallCategory.getName(), mallCategory.getKeywords(), mallCategory.getDesc(), mallCategory.getIconUrl(), mallCategory.getPicUrl(), mallCategory.getLevel(), categoryListVo1List);
            categoryListVoList.add(categoryListVo);

        }


        return categoryListVoList;
    }

    /**
     * @Author chaohe
     * @description: 商品类目的一级目录回显
     * @Date 15:02 2021/7/10
     * url:http://localhost:8083/admin/category/l1
     */

    @Override
    public List<CategoryL1Vo> l1() {
        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        List<MallCategory> mallCategoryList = mallCategoryMapper.selectByExample(mallCategoryExample);

        List<CategoryL1Vo> categoryL1Vos = new ArrayList<>();
        for (MallCategory mallCategory : mallCategoryList) {
            CategoryL1Vo categoryL1Vo = new CategoryL1Vo(mallCategory.getId(), mallCategory.getName());
            categoryL1Vos.add(categoryL1Vo);

        }

        return categoryL1Vos;
    }

    /**
     * @Author chaohe
     * @description: 商品类目的删除，注意是逻辑删除
     * @Date 15:04 2021/7/10
     * url:  http://localhost:8083/admin/category/delete
     */

    @Override
    public void delete(CategoryDeleteBo categoryDeleteBo) {
        //判断请求参数中的children是不是为空
        if (categoryDeleteBo.getChildren() != null) {
            List<CategoryDeleteBo1> children = categoryDeleteBo.getChildren();
            //如果不为空，则循环读出并逻辑删除操作
            for (CategoryDeleteBo1 child : children) {
//            MallCategoryExample mallCategoryExample = new MallCategoryExample();
//            MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();
//            criteria.andIdEqualTo(child.getId());
                MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(child.getId());
                MallCategory mallCategory1 = new MallCategory(child.getId(), child.getName(), child.getKeywords(), child.getDesc(), mallCategory.getPid(), child.getIconUrl(), child.getPicUrl(), child.getLevel(), mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), 1);

                MallCategoryExample mallCategoryExample = new MallCategoryExample();
                MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();
                criteria.andIdEqualTo(child.getId());

                mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);
            }


        }
        //如果children为空的话，继续逻辑删除
        MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(categoryDeleteBo.getId());
        MallCategory mallCategory1 = new MallCategory(categoryDeleteBo.getId(), categoryDeleteBo.getName(), categoryDeleteBo.getKeywords(), categoryDeleteBo.getDesc(), mallCategory.getPid(), categoryDeleteBo.getIconUrl(), categoryDeleteBo.getPicUrl(), categoryDeleteBo.getLevel(), mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), 1);

        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();
        criteria.andIdEqualTo(categoryDeleteBo.getId());
        mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);


    }


    /**
     * @Author chaohe
     * @description:商品类目中的新增
     * @Date 15:09 2021/7/10
     * url:  http://localhost:8083/admin/category/create
     */
    @Override
    public CategoryCreateVo create(CategoryCreateBo categoryCreateBo) {

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());

        MallCategory mallCategory = new MallCategory();
        mallCategory.setName(categoryCreateBo.getName());
        mallCategory.setKeywords(categoryCreateBo.getKeywords());
        mallCategory.setLevel(categoryCreateBo.getLevel());
        mallCategory.setPid(categoryCreateBo.getPid());
        mallCategory.setDesc(categoryCreateBo.getDesc());
        mallCategory.setIconUrl(categoryCreateBo.getIconUrl());
        mallCategory.setPicUrl(categoryCreateBo.getPicUrl());
        mallCategory.setAddTime(timestamp);
        mallCategory.setUpdateTime(timestamp);

        int i = mallCategoryMapper.insertSelective(mallCategory);

        //如果插入成功的话
        CategoryCreateVo categoryCreateVo = null;
        if (i == 1) {
            //useGeneratedKeys="true" keyProperty="id"这里用到了，作用是得到刚插入进去的数据的主键Id
            Integer id = mallCategory.getId();
            MallCategory mallCategory1 = mallCategoryMapper.selectByPrimaryKey(id);
            categoryCreateVo = new CategoryCreateVo(mallCategory1.getId(), mallCategory1.getName(), mallCategory1.getKeywords(), mallCategory1.getDesc(), mallCategory1.getPid(), mallCategory1.getIconUrl(), mallCategory1.getPicUrl(), mallCategory1.getLevel(), mallCategory1.getAddTime(), mallCategory1.getUpdateTime());

        }
        return categoryCreateVo;
    }

    /**
     * @Author chaohe
     * @description: 商品类目的修改
     * @Date 15:11 2021/7/10
     * url: http://localhost:8083/admin/category/update
     */

    @Override
    public void update(CategoryUpdateBo categoryUpdateBo) {

        Integer pid = categoryUpdateBo.getPid();

        List<CategoryUpdateBo1> children = categoryUpdateBo.getChildren();

        String level = categoryUpdateBo.getLevel();


        if (children == null) {

            MallCategoryExample mallCategoryExample = new MallCategoryExample();
            MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();

            MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(categoryUpdateBo.getId());

            MallCategory mallCategory1 = new MallCategory(categoryUpdateBo.getId(), categoryUpdateBo.getName(), categoryUpdateBo.getKeywords(), categoryUpdateBo.getDesc(), categoryUpdateBo.getPid(), categoryUpdateBo.getIconUrl(), categoryUpdateBo.getPicUrl(), categoryUpdateBo.getLevel(), mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), mallCategory.getDeleted());

            criteria.andIdEqualTo(categoryUpdateBo.getId());

            mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);
            return;
        }
        if (children != null && pid == null) {

            MallCategoryExample mallCategoryExample = new MallCategoryExample();
            MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();

            MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(categoryUpdateBo.getId());

            MallCategory mallCategory1 = new MallCategory(categoryUpdateBo.getId(), categoryUpdateBo.getName(), categoryUpdateBo.getKeywords(), categoryUpdateBo.getDesc(), mallCategory.getPid(), categoryUpdateBo.getIconUrl(), categoryUpdateBo.getPicUrl(), categoryUpdateBo.getLevel(), mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), mallCategory.getDeleted());

            criteria.andIdEqualTo(categoryUpdateBo.getId());

            mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);
            return;

        }
        if (children != null && pid != null) {
            MallCategoryExample mallCategoryExample = new MallCategoryExample();
            MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();

            MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(categoryUpdateBo.getId());

            MallCategory mallCategory1 = new MallCategory(categoryUpdateBo.getId(), categoryUpdateBo.getName(), categoryUpdateBo.getKeywords(), categoryUpdateBo.getDesc(), categoryUpdateBo.getPid(), categoryUpdateBo.getIconUrl(), categoryUpdateBo.getPicUrl(), categoryUpdateBo.getLevel(), mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), mallCategory.getDeleted());

            criteria.andIdEqualTo(categoryUpdateBo.getId());

            mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);
            for (CategoryUpdateBo1 child : children) {
                MallCategoryExample mallCategoryExample1 = new MallCategoryExample();
                MallCategoryExample.Criteria criteria1 = mallCategoryExample1.createCriteria();

                MallCategory mallCategory2 = mallCategoryMapper.selectByPrimaryKey(child.getId());

                MallCategory mallCategory3 = new MallCategory(child.getId(), child.getName(), child.getKeywords(), child.getDesc(), mallCategory2.getPid(), child.getIconUrl(), child.getPicUrl(), child.getLevel(), mallCategory2.getSortOrder(), mallCategory2.getAddTime(), mallCategory2.getUpdateTime(), 1);

                criteria1.andIdEqualTo(child.getId());

                mallCategoryMapper.updateByExample(mallCategory3, mallCategoryExample1);

            }
            return;
        }

    }
}
//        MallCategoryExample mallCategoryExample = new MallCategoryExample();
//        MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();
//
//        //判断children是不是空
//        if (cateUpdateBoo.getChildren() != null) {
//            List<CategoryUpdateBo1> children = cateUpdateBoo.getChildren();
//            //是bu空的话循环修改
//            for (CategoryUpdateBo1 child : children) {
//                MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(child.getId());
//                MallCategory mallCategory1 = new MallCategory(child.getId(), child.getName(), child.getKeywords(), child.getDesc(), mallCategory.getPid(), child.getIconUrl(), child.getPicUrl(), child.getLevel(), mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), mallCategory.getDeleted());
//
//
//                criteria.andIdEqualTo(child.getId());
//                mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);
//            }
//        }
//        //pid是不是为空
//        if (pid != null ) {
//
//            MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(cateUpdateBoo.getId());
//            //这里主要是为了修改，特别是pid，
//            MallCategory mallCategory1 = new MallCategory(cateUpdateBoo.getId(), cateUpdateBoo.getName(), cateUpdateBoo.getKeywords(), cateUpdateBoo.getDesc(), pid, cateUpdateBoo.getIconUrl(), cateUpdateBoo.getPicUrl(), cateUpdateBoo.getLevel(), mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), mallCategory.getDeleted());
//
//            criteria.andIdEqualTo(cateUpdateBoo.getId());
//
//            int i = mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);
//
//            if (i == 1) {
//
//                return;
//            }
//        }
//        //如果pid为空或者pid=0时，给默认设置为0，方便回显
//            MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(cateUpdateBoo.getId());
//            MallCategory mallCategory1 = new MallCategory(cateUpdateBoo.getId(), cateUpdateBoo.getName(), cateUpdateBoo.getKeywords(), cateUpdateBoo.getDesc(), 0, cateUpdateBoo.getIconUrl(), cateUpdateBoo.getPicUrl(), "L1", mallCategory.getSortOrder(), mallCategory.getAddTime(), mallCategory.getUpdateTime(), mallCategory.getDeleted());
//
//
//            criteria.andIdEqualTo(cateUpdateBoo.getId());
//            mallCategoryMapper.updateByExample(mallCategory1, mallCategoryExample);
//            return;
//    }


